package com.situ.hospital.dao.Impl;

import com.situ.hospital.dao.IOrderDAO;
import com.situ.hospital.pojo.Order;
import com.situ.hospital.pojo.query.OrderQuery;
import com.situ.hospital.pojo.vo.OrderVO;
import com.situ.hospital.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class OrderDAOImpl implements IOrderDAO {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<OrderVO> selectByPage(OrderQuery orderQuery) {
        String sql = "SELECT o.id,d.`name` AS departmentName,u.`name` AS userName,p.`name` AS patientName,m.`name` AS medicineName,o.number,o.create_time,o.update_time" +
                " FROM orders AS o" +
                " LEFT JOIN user AS u ON o.user_id=u.id" +
                " LEFT JOIN department AS d ON u.department_id=d.id" +
                " LEFT JOIN patient AS p ON o.patient_id=p.id" +
                " LEFT JOIN medicine AS m ON o.medicine_id=m.id" +
                " WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        Integer queryDepartmentId = orderQuery.getDepartmentId();
        if(queryDepartmentId != null) {
            sql += " AND u.department_id=?";
            queryList.add(queryDepartmentId);
        }
        Integer queryUserId = orderQuery.getUserId();
        if(queryUserId != null) {
            sql += " AND o.user_id=?";
            queryList.add(queryUserId);
        }
        Integer queryPatientId = orderQuery.getPatientId();
        if(queryPatientId != null) {
            sql += " AND o.patient_id=?";
            queryList.add(queryPatientId);
        }
        Integer queryMedicineId = orderQuery.getMedicineId();
        if(queryMedicineId != null) {
            sql += " AND o.medicine_id=?";
            queryList.add(queryMedicineId);
        }
        sql += " LIMIT ?,?";
        int offset = (orderQuery.getPage() - 1) * orderQuery.getLimit();
        queryList.add(offset);
        queryList.add(orderQuery.getLimit());
        return template.query(sql, new BeanPropertyRowMapper<>(OrderVO.class), queryList.toArray());
    }

    @Override
    public int selectTotalCount(OrderQuery orderQuery) {
        String sql = "SELECT COUNT(*) FROM orders AS o" +
                " LEFT JOIN user AS u ON o.user_id=u.id" +
                " LEFT JOIN department AS d ON u.department_id=d.id" +
                " LEFT JOIN patient AS p ON o.patient_id=p.id" +
                " LEFT JOIN medicine AS m ON o.medicine_id=m.id" +
                " WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        Integer queryDepartmentId = orderQuery.getDepartmentId();
        if(queryDepartmentId != null) {
            sql += " AND u.department_id=?";
            queryList.add(queryDepartmentId);
        }
        Integer queryUserId = orderQuery.getUserId();
        if(queryUserId != null) {
            sql += " AND o.user_id=?";
            queryList.add(queryUserId);
        }
        Integer queryPatientId = orderQuery.getPatientId();
        if(queryPatientId != null) {
            sql += " AND o.patient_id=?";
            queryList.add(queryPatientId);
        }
        Integer queryMedicineId = orderQuery.getMedicineId();
        if(queryMedicineId != null) {
            sql += " AND o.medicine_id=?";
            queryList.add(queryMedicineId);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        String sql = "DELETE FROM orders WHERE id=?";
        template.update(sql, id);
    }

    @Override
    public void add(Order order) {
        String sql = "INSERT INTO orders(patient_id,user_id,medicine_id,number) VALUES(?,?,?,?)";
        template.update(sql, order.getPatientId(), order.getUserId(), order.getMedicineId(),order.getNumber());
    }

    @Override
    public Order selectById(int id) {
        String sql = "SELECT id,patient_id,user_id,medicine_id,number,create_time,update_time FROM orders WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Order.class), id);
    }

    @Override
    public void update(Order order) {
        String sql = "UPDATE orders SET patient_id=?,user_id=?,medicine_id=?,number=? WHERE id=?";
        template.update(sql, order.getPatientId(), order.getUserId(), order.getMedicineId(),order.getNumber(), order.getId());
    }

    @Override
    public Integer selectByMedicineId(int id) {
        String sql = "SELECT SUM(number) FROM orders WHERE medicine_id=?";
        return template.queryForObject(sql, Integer.class, id);
    }
}
